package main

import (
	"fmt"
)

type Books struct {
	bookId  int
	title   string
	author  string
	subject string
}

func main() {

	// 创建结构体
	fmt.Println(Books{1, "Go语言", "www.go.com", "Go语言教程"})
	/* 也可以使用 key => value 格式 */
	fmt.Println(Books{bookId: 2, title: "Python语言", author: "www.Python.com", subject: "Python语言语言教程"})
	/* 忽略字段为空或零 */
	fmt.Println(Books{title: "C语言", author: "www.C.com"})

	// 访问结构体成员
	var Book1 Books
	var Book2 Books
	/* book 1 描述 */
	Book1.title = "Go 语言"
	Book1.author = "www.Go.com"
	Book1.subject = "Go 语言教程"
	Book1.bookId = 3

	/* book 2 描述 */
	Book2.title = "Python 教程"
	Book2.author = "www.Python.com"
	Book2.subject = "Python 语言教程"
	Book2.bookId = 4

	/* 打印 Book1 信息 */
	fmt.Printf("Book 1 title : %s\n", Book1.title)
	fmt.Printf("Book 1 author : %s\n", Book1.author)
	fmt.Printf("Book 1 subject : %s\n", Book1.subject)
	fmt.Printf("Book 1 book_id : %d\n", Book1.bookId)

	/* 打印 Book2 信息 */
	fmt.Printf("Book 2 title : %s\n", Book2.title)
	fmt.Printf("Book 2 author : %s\n", Book2.author)
	fmt.Printf("Book 2 subject : %s\n", Book2.subject)
	fmt.Printf("Book 2 book_id : %d\n", Book2.bookId)

	/* 结构体作为函数参数 */
	printBooks(Book1)
	printBooks(Book2)

	/* 打印 Book1 信息 */
	printBooksPointer(&Book1)

	/* 打印 Book2 信息 */
	printBooksPointer(&Book2)
}

/*
	结构体作为函数参数
*/
func printBooks(book Books) {
	fmt.Printf("Book title : %s\n", book.title)
	fmt.Printf("Book author : %s\n", book.author)
	fmt.Printf("Book subject : %s\n", book.subject)
	fmt.Printf("Book book_id : %d\n", book.bookId)
}

/*
	结构体作为函数参数
*/
func printBooksPointer(book *Books) {
	fmt.Printf("Book title : %s\n", book.title)
	fmt.Printf("Book author : %s\n", book.author)
	fmt.Printf("Book subject : %s\n", book.subject)
	fmt.Printf("Book book_id : %d\n", book.bookId)
}
